<!--
 * @Descripttion: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2021-12-17 09:06:18
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2021-12-17 09:20:54
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>完全二叉树点数求解</title>
</head>

<body>
    <script>
        //已知一个二叉树的节点，求二叉树满二叉树的节点个数
        // 1、递归方法
        function getNodes(root) {
            let nodeNum = 0;
            if (root == null) {
                return nodeNum;
            }
            const getNodeSum = function (node) {
                if (node == null) {
                    return 0;
                }
                let leftNum = getNodeSum(node.left);
                let rightNum = getNodeSum(node.right);
                return leftNum + rightNum;
            }
            return getNodeSum(root);

        }

        // 2、层序法
        function getNodes(root) {
            let queue = [];
            if (root === null) {
                return 0;
            }
            queue.push(root);
            let nodeNum = 0;
            while (queue.length) {
                let length = queue.length;
                while (length--) {
                    let node = queue.shift();
                    nodeNum++;
                    node.left && queue.push(node.left);
                    node.right && queue.push(node.right);
                }
            }
            return nodeNum;
        }
    </script>
</body>

</html>